package com.yy.yyui.text
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.text.TextFieldType;
	import flash.text.TextFormatAlign;
	
	import com.util.tools.GT;
	import com.maqie.view.UM;
	import com.util.tools.StringTools;

	public class TextInput extends Sprite
	{
		private var tl:TextLabel;
		private var bg:DisplayObject;
		private var _actions:Object;
		private var _autoSelect:Object;
		public function TextInput(txt:String="",width:Number=100,lines:uint = 1,background:DisplayObject = null,align:String = TextFormatAlign.LEFT,endbel:Boolean = true)
		{
			//TODO: implement function
			super();
			if(background==null){
				bg = UM.defaultContainerBackgroundDarker;
				bg.height +=5;
				bg.visible = false;
			}else
			{
				bg = background;
			}
			this.addChild(bg);
			bg.width = width;
			this.scrollRect = new Rectangle(0,0,bg.width,bg.height);
			tl = new TextLabel(txt,width,1);
			tl.textFormat.align = align;
			//tl.transform.colorTransform = new ColorTransform(-0.3,-0.3,-0.3,1,0xff,0xff,0xff,-0.5);
			tl.width = bg.width;
			this.addChild(tl);
			if(endbel)
			{
				tl.selectable = true;
				//set action
				tl.addEventListener(Event.CHANGE,inputHandler);
				this._actions = {};
				tl.addEventListener(MouseEvent.CLICK,clickHandler);
				
				
				//tl.addEventListener(TextEvent.TEXT_INPUT,update);
				tl.addEventListener(FocusEvent.FOCUS_IN,onFocus);
				tl.addEventListener(FocusEvent.FOCUS_OUT,outFocus);
				//tl.addEventListener(TextLabel.SIZECHANGE,update);
				this.enableInput = true;
				update();
			}
		}
//		
		private function clickHandler(e:MouseEvent):void{
			if(this.autoSelect){
				tl.setSelection(0,tl.text.length);
			}
			tl.text = tl.text;
		}
		
		public function set autoSelect(value:Boolean):void{_autoSelect = value;}
		public function get autoSelect():Boolean{return this._autoSelect;}
		
		public function set inputColor(value:uint):void{
			this.input.transform.colorTransform = new ColorTransform();
			this.input.textFormat.color = value;
		}
		
		public function get onChange():Function{
			return this.actions.onChange;
		}
		
		public function set onChange(value:Function):void{
			this.actions.onChange = value;
		}
		
		private function inputHandler(e:Event):void{
			if(this.textLabel.maxChars>0){
				if(this.textLabel.maxChars<StringTools.getStringLengthByCodes(this.textLabel.text)){
					var str:String = this.textLabel.text;
					do{
						str = str.substr(0,str.length-1);
					}while(StringTools.getStringLengthByCodes(str)>this.textLabel.maxChars)
					this.textLabel.textNoNotify = str;
				}
				//this.textLabel.get.text
			}
			this.onChange(e);
			this.update(e);
			
		}
		
		public function get actions():Object{
			return this._actions;
		}
		
		public function onFocus(e:FocusEvent = null):void{
			if(this.enableInput){
				bg.transform.colorTransform = new ColorTransform(1,1,1,1,0x40,0x40,0x40);
			}
		}

		public function outFocus(e:FocusEvent = null):void{
			if(this.enableInput){
				bg.transform.colorTransform = new ColorTransform();
			}
		}
		
		public function get input():TextLabel{
			return this.tl;
		}
		public function update(e:Event = null):void{
			
			tl.text = tl.text;
//			bg.width = tl.width;
//			bg.height = tl.height;
		} 
		
		override public function get width():Number{
			return this.scrollRect.width;
		}
		
		override public function get height():Number{
			return this.scrollRect.height;
		}

		override public function set width(newWidth:Number):void{
			var diff:Number = newWidth - this.width;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			this.tl.width += diff;
			this.bg.width += diff;
			
			var sRect:Rectangle = this.scrollRect;
			sRect.width += diff;
			this.scrollRect = sRect;
			
		}
		
		override public function set height(newHeight:Number):void{
			newHeight+=2;
			var diff:Number = newHeight - this.height;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;
			
			this.tl.height += diff;
			this.bg.height += diff;			

			var sRect:Rectangle = this.scrollRect;
			sRect.height+=diff;
			this.scrollRect = sRect;
		}
		
		public function get text():String{
			return this.tl.text;
		}
		
		public function set text(value:String):void{
			this.tl.text = value;
		}
		
		public function get enableInput():Boolean{
			return this.tl.type==TextFieldType.INPUT;
		}
		
		public function set enableInput(value:Boolean):void{
//			this.bg.alpha = value?1:0;
			this.tl.type = value?TextFieldType.INPUT:TextFieldType.DYNAMIC;
		}
		
		public function get background():DisplayObject{
			return this.bg;
		}
		
		public function get textLabel():TextLabel{
			return this.tl;
		}
		
		//<!- modify by yaoy 2009,10.17 (字符长度) ----------------------------------------------------------
        public function get maxChars():int
        {
            return this.tl.maxChars;
        }  
        
        public function set maxChars(value:int):void
        {
            this.tl.maxChars = value;
        }
/*             if(maxChars < 0)
            {
                removeEventListener(TextEvent.TEXT_INPUT,textInput);      
            }else
            {
                addEventListener(TextEvent.TEXT_INPUT,textInput);
                text = getTextByCharLength(text,maxChars);
            } 
        }*/
        
/*         private function textInput(e:TextEvent):void
        {
            //拦截并阻止textinput事件,手动处理内容输入
            var textField:TextField = e.currentTarget as TextField;
            //DT.t(e.text);
            var temp:String = getTextByCharLength(e.text, maxChars - this.tl.length + getCharLength(this.tl.selectedText));
            var index:int = selectionBeginIndex;
            replaceText(selectionBeginIndex,selectionEndIndex,temp);
            setSelection(index+temp.length,index+temp.length);
            dispatchEvent(new Event(Event.CHANGE,true));
            e.preventDefault();
        }  */
    
		// !->----------------------------------------------------------------------		
		
		// modify by yaoy 2009.08.21 (密码显示)
		public function set displayAsPassword(value:Boolean):void
		{
			this.tl.displayAsPassword = value;
		}
		
		// modify by yaoy 2009,10.17 (字符长度)
		public function get length(): int
		{
			return this.textLabel.length;
		}
		
		public function set restrict(str:String):void {
			this.tl.restrict = str;
		}
		public function get restrict():String {
			return this.tl.restrict;
		} 
		
		public function set wordWarp(wp:Boolean):void {
			this.tl.wordWrap = wp;
		}
		public function get wordWarp():Boolean {
			return this.tl.wordWrap;
		}
	
	}
}